package smartcase.base.log;

import org.slf4j.Logger;
import smartcase.base.access.LogCtx;

import static smartcase.base.ApplicationContextProviderKt.getCommonInfoFromContext;
import static smartcase.common.util.JsonUtil.toJsonString;

final class LogImpl implements Log {
  private final Logger logger;

  LogImpl(Logger logger) {
    this.logger = logger;
  }

  private static String getExtLog(LogCtx cInfo) {
    if (cInfo == null) cInfo = new LogCtx();
    return String.format(
        "| commonLogCtx:requestId=[%s], path=[%s], endpoint=[%s], extInfo=[%s]",
        cInfo.getRequestId(),
        cInfo.getPath(),
        cInfo.getEndpoint(),
        toJsonString(cInfo.getExtInfo()));
  }

  @Override
  public void info(LogCtx cInfo, String fmtString, Object... data) {
    logger.info(fmtString + getExtLog(cInfo), data);
  }

  @Override
  public void info(String fmtString, Object... data) {
    this.info(getCommonInfoFromContext(), fmtString, data);
  }

  @Override
  public void debug(String fmtString, Object... data) {
    this.debug(getCommonInfoFromContext(), fmtString, data);
  }

  @Override
  public void debug(LogCtx cInfo, String fmtString, Object... data) {
    logger.debug(fmtString + getExtLog(cInfo), data);
  }

  @Override
  public void error(String fmtString, Object... data) {
    this.error(getCommonInfoFromContext(), fmtString, data);
  }

  @Override
  public void error(LogCtx cInfo, String fmtString, Object... data) {
    logger.error(fmtString + getExtLog(cInfo), data);
  }

  @Override
  public void warn(String fmtString, Object... data) {
    this.warn(getCommonInfoFromContext(), fmtString, data);
  }

  @Override
  public void warn(LogCtx cInfo, String fmtString, Object... data) {
    logger.warn(fmtString + getExtLog(cInfo), data);
  }

  @Override
  public void trace(String fmtString, Object... data) {
    this.trace(getCommonInfoFromContext(), fmtString, data);
  }

  @Override
  public void trace(LogCtx cInfo, String fmtString, Object... data) {
    logger.trace(fmtString + getExtLog(cInfo), data);
  }
}
